package com.yanqu.road.server.gameplayer.module.activity;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.activity.*;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnUserData;
import com.yanqu.road.entity.config.goods.GetRechargeMallResult;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.activity.eActivityChargeType;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.mailsub.eMailSubscribeType;
import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.entity.enums.rank.eRankRangeType;
import com.yanqu.road.entity.player.UserAttribute;
import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.entity.servercenter.IntroductionShieldConfig;
import com.yanqu.road.entity.task.args.RechargeDaysArgs;
import com.yanqu.road.entity.task.args.union.UnionRechargeUserArgs;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.helper.UnionActivityTaskConditionHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.*;
import com.yanqu.road.pb.activity.union.UnionRechargeUserProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.newserverwelfare.NewServerWelfareModule;
import com.yanqu.road.server.gameplayer.module.activity.zhanling.ZhanLingActivityModule;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.*;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.recharge.RechargeManager;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.*;
import com.yanqu.road.server.manger.activity.able.ActivityAbleMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.activity.oldplayerreturn.OldPlayerReturnMgr;
import com.yanqu.road.server.manger.activity.platform.IntroductionShieldMgr;
import com.yanqu.road.server.manger.activity.platform.PlatformActivityMgr;
import com.yanqu.road.server.manger.activity.qqprivilege.QQPrivilegeMgr;
import com.yanqu.road.server.manger.activity.shushan.ShuShanRankActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class ActivityModule extends GeneralModule {

    private Map<Integer, BaseActivityData> baseActivityDataMap;
    private Map<Integer, List<UserActivityConditionData>> userActivityConditionDataMap;
    /**
     * 临时改变的任务列表
     */
    private ArrayList<BaseActivityData> changeBaseActivityDataList = new ArrayList<>();

    private AtomicInteger changeBaseActivityCount = new AtomicInteger();

    public ActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        List<Integer> activityIdList = new ArrayList<>(NormalActivityMgr.getShowTimeActivityInfoMap().keySet());
        baseActivityDataMap = new ConcurrentHashMap<>();
        userActivityConditionDataMap = UserActivityBussiness.getUserActivityConditionDataMap(player.getUserId(), activityIdList);
        return true;
    }

    @Override
    public boolean afterLoadData() {
        //填充baseActivityDataMap
        for (Map.Entry<Integer, List<UserActivityConditionData>> dataEntry : userActivityConditionDataMap.entrySet()) {
            addActivityData(NormalActivityMgr.getActivityInfo(dataEntry.getKey()), dataEntry.getValue(), false);
        }
        //检测新活动
        checkNewActivityData();
        //重置检测
        resetOneDay();
        userActivityConditionDataMap = null;
        return false;
    }

    @Override
    public boolean saveData() {
        if (null != baseActivityDataMap) {
            List<BaseActivityData> tempActivityList = new ArrayList<>(baseActivityDataMap.values());
            List<UserActivityConditionData> tempAddUserActivityConditionDatas = new ArrayList<>();
            for (BaseActivityData baseActivityData : tempActivityList) {
                if (NormalActivityMgr.isMonopolyMarketRankActivity(baseActivityData.getActivityInfo()) || baseActivityData.getActivityInfo().getType() == eActivityType.ShuShanActivity.getValue()) {
                    continue;
                }
                for (UserActivityConditionData userActivityConditionData : baseActivityData.getUserActivityConditionDataList()) {
                    if (userActivityConditionData.isInsertOption()) {
                        // 初始值不写入
//                        if (player.isNewRoleNotSave() && userActivityConditionData.isInitData()) {
//                            continue;
//                        }
                        tempAddUserActivityConditionDatas.add(userActivityConditionData);
                    } else if (userActivityConditionData.isUpdateOption()) {
                        UserActivityBussiness.updateUserActivityConditionData(userActivityConditionData);
                    }
                }
            }
            UserActivityBussiness.batchAddUserActivityConditionData(tempAddUserActivityConditionDatas);
        }
        return true;
    }

    @Override
    public void loginSendMsg() {
        //检测新活动
        checkNewActivityData();
        checkUnionActivityData();
        syncActivity();
        syncSpecialActivityData();
        syncTurntableLotteryRecord();
        player.notifyListener(eGamePlayerEventType.PaintedEggsEvent.getValue());
    }

    @Override
    public void afterLogin() {
        syncIntroductionShield();
        player.getModule(ChannelActivityModule.class).syncCrossChannelServerName();
        //登录更新一下全服进度
        player.notifyListener(eGamePlayerEventType.SeasonServerMallBuyTask.getValue());
    }

    public void syncActivity() {
        if (null != baseActivityDataMap) {
            String language = player.getLanguage();
            List<ActivityInfo> normalActivityList = new ArrayList<>();
            int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
            long nowTime = DateHelper.getCurrentSecond();
            for (ActivityInfo activityInfo : NormalActivityMgr.getShowTimeActivityInfoMap().values()) {
                //代金币礼包游戏内不显示
                if (activityInfo.getType() == eActivityType.RechargeActivity.getValue() && activityInfo.getChildType() == eActivityChargeType.GoldTokenCharge.getChildType()) {
                    continue;
                }
                if (nowTime < (activityInfo.getBeginShowTime() - DateHelper.MINUTE_SECONDS * 10)) {
                    continue;
                }
                if (activityInfo.getType() == eActivityType.MailSub37.getValue() && isMailSub(channelId)) {
                    continue;
                }
//                if (!eChannelType.isXiaoChengxuChannel(channelId)) {
//                    // 小程序渠道不做判断
//                    if (activityInfo.getType() == eActivityType.PlatformActivity.getValue() && activityInfo.getChildType() == 23) {
//                        //如果登陆的是 h5 平台， 就判断当前的系统
//                        if (Objects.equals(player.getAppId(), "37h5_cn")) {
//                            if (player.getPhonePlatform() != 1) {
//                                //不是安卓
//                                continue;
//                            }
//                        }else if (player.getUserInfo().getAppId() != null && !player.getUserInfo().getAppId().equals("37h5")) {
//                            //只有注册的时候是 37h5 的才下发
//                            continue;
//                        }
//                    }
//                    if (activityInfo.getType() == eActivityType.PlatformActivity.getValue() && activityInfo.getChildType() == 31) {
//                        if (Objects.equals(player.getAppId(), "37h5_cn")) {
//                            if (player.getPhonePlatform() != 2) {
//                                //不是安卓
//                                continue;
//                            }
//                        }else  if (player.getUserInfo().getAppId() != null
//                                && !player.getUserInfo().getAppId().equals("iOS")
//                                && !player.getUserInfo().getAppId().equals("iOSx")
//                        ) {
//                            //只有注册的时候是iOS 的才下发
//                            continue;
//                        }
//                    }
//                }
//
//                if (activityInfo.getType() == eActivityType.PlatformActivity.getValue() && activityInfo.getChildType() == 45) {
//                    //分渠道 目前叫 OPPO 大奖赛
//                    String param6 = activityInfo.getParam6();
//                    if (!StringUtils.isNullOrEmpty(param6) && !Objects.equals("0",param6)) {
//                        List<String> strings = StringUtils.stringToStringList(param6, ",");
//                        String appId = player.getUserInfo().getAppId();
//                        if (appId != null && !strings.contains(appId)) {
//                            continue;
//                        }
//                    }
//                }
                List<Integer> childTypes = Arrays.asList(68, 69, 72, 74); // 子活动类型 74 -- 返利活动海报

                if (activityInfo.getType() == eActivityType.PlatformActivity.getValue()
                        && childTypes.contains(activityInfo.getChildType())) {
                    PlatformActivityMgr.syncPosterActivityConfig(player);
                }

                if (activityInfo.getType() == eActivityType.PlatformActivity.getValue() && activityInfo.getChildType() == 28) {
                    try {
                        List<String> childGameIdList = StringUtils.stringToStringList(activityInfo.getParam4(), ";");
                        if (player.getChildGameId() == null || player.getUserInfo().getChildGameId() == null) {
                            continue;
                        }
                        if (!player.getChildGameId().equals(player.getUserInfo().getChildGameId())) {
                            continue;
                        }
                        if (!childGameIdList.contains(player.getUserInfo().getChildGameId())) {
                            continue;
                        }
                    } catch (Exception ex) {
                        log.error("Activity Exception:", ex);
                        continue;
                    }
                }
                // QQ 游戏大厅特权活动
                if (activityInfo.getType() == eActivityType.QQPrivilege.getValue() && !QQPrivilegeMgr.isAble(player.getAppId(), channelId)) {
                    continue;
                }
                //重归汴梁活动是否不需要同步
                if (isOldPlayerReturnNoSync(activityInfo)) {
                    continue;
                }


                // 新版平台活动判断
                if (activityInfo.getType() == eActivityType.PlatformActivity.getValue() || activityInfo.getType() == eActivityType.ZhangGuiActivity.getValue()) {
                    try {
                        if (player.getAppExt() != null && player.getAppExt().containsKey("isH5")) {
                            int clientType = player.getAppExt().getIntValue("isH5");
                            if (!PlatformActivityMgr.isSyncPlatformActivity(activityInfo.getType(), activityInfo.getChildType(), player.getAppId(), clientType, player.getPhonePlatform())) {
                                continue;
                            }
                        } else {
                            continue;
                        }
                    } catch (Exception ex) {
                        log.error("clientType isH5 Exception", ex);
                        continue;
                    }
                }

                normalActivityList.add(activityInfo);
            }

            player.sendPacket(Protocol.U_ACTIVITY_LIST_ZIP, ActivityPb.parseActivityDataListMsg(new ArrayList<>(baseActivityDataMap.values()),
                    normalActivityList, NormalActivityMgr.getActivityConditionInfoMap(),
                    NormalActivityMgr.getAllActivityMallInfoMap(), NormalActivityMgr.getAllActivityConfigMap(), language));
        }
    }

    public boolean isMailSub(int channelId) {
        // 渠道 ID 判断
        if (channelId != eChannelType.TaiWan.getValue()
                && channelId != eChannelType.Japan.getValue()
                && channelId != eChannelType.Korea.getValue()
                && channelId != eChannelType.Thailand.getValue()
                && channelId != eChannelType.US.getValue()
                && channelId != eChannelType.UK.getValue()
                /*&& channelId != eChannelType.VN.getValue()*/) {
            // 如果不属于这些渠道，则不显示活动入口
            return true;
        }

        // 增加个频率限制，每天请求一次就好
        Map<ePlayerAttrType, UserAttribute> userAttributeMap = player.getModule(AttributeModule.class).getUserAttributeMap();
        if (userAttributeMap != null
                && userAttributeMap.containsKey(ePlayerAttrType.MailSub37)
                && System.currentTimeMillis() - userAttributeMap.get(ePlayerAttrType.MailSub37).getLastUpdateTime() < DateHelper.DAY_MILLIONS) {
           return player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.MailSub37) == 1;
        }

        if (player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.MailSub37) == 1) {
            return true;
        }

        return GetMailSub(eMailSubscribeType.forNumber(channelId), player.getUserInfo().getPlatformUserId());
    }

    public boolean GetMailSub(eMailSubscribeType eType, String platformUserId) {
        if (eType == null || platformUserId == null || platformUserId.isEmpty()) {
            // 出问题，就不要展示活动入口
            return true;
        }

        try {
            String url = eType.getUrl();
            Map<String, String> paramMap = new TreeMap<>();
            paramMap.put("game_id", String.valueOf(eType.getGameId()));
            paramMap.put("uid", platformUserId);

            Map<String, String> resp = HttpHelper.doGet(url, paramMap);
            if (resp == null || resp.isEmpty()) {
                log.error("获取玩家邮件订阅失败，platformUserId:{},msg:{}", platformUserId, resp);
                return true;
            }
            String result = resp.get("body");
            if (result == null || result.isEmpty()) {
                return true;
            }
            JSONObject resultObject = JSONObject.parseObject(result);
            if (resultObject == null) {
                log.error("获取玩家邮件订阅失败，platformUserId:{},msg:{}", platformUserId, result);
                return true;
            }
            int code = resultObject.getIntValue("code");
            if (code != 1) {
                log.error("获取玩家邮件订阅失败，platformUserId:{},code:{},msg:{}", platformUserId, code, result);
                return true;
            }
            JSONObject data = resultObject.getJSONObject("data");
            if (data == null) {
                log.error("获取玩家邮件订阅失败，platformUserId:{},code:{},msg:{}", platformUserId, code, result);
                return true;
            }
            int collectState = data.getIntValue("collect_state");
            player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.MailSub37, collectState);
            return collectState == 1;
        } catch (Exception e) {
            log.error("获取玩家邮件订阅失败，platformUserId:{}", platformUserId, e);
            return true;
        }
    }

    /**
     * 重归汴梁活动是否不需要同步
     * @param activityInfo 活动信息
     * @return bool
     */
    private boolean isOldPlayerReturnNoSync(ActivityInfo activityInfo) {
        //重归汴梁活动
        if (activityInfo.getType() == eActivityType.OldPlayerReturn.getValue()) {
            try {
                //不是新区或者不在活动有效期内
                if (!OldPlayerReturnMgr.checkOpenServerDays(player.getUserInfo().getRegisterTime())) {
                    OldPlayerReturnUserData userData = OldPlayerReturnMgr.getUserData(getUserId());
                    if (userData == null) {
                        return true;
                    }
                    //是回归角色 并且在回归有效期 要同步
                    return !userData.isReturn() || !OldPlayerReturnMgr.checkReturnEffectiveTime(userData.getReturnTime()) || userData.getActivityId() != activityInfo.getActivityId();
                } else {
                    //新区的玩家已经绑定了 但不是这个活动绑定的
                    OldPlayerReturnUserData userData = OldPlayerReturnMgr.getUserData(getUserId());
                    if (userData != null) {
                        if (userData.isAlreadyBind() && userData.getActivityId() != activityInfo.getActivityId()) {
                            return true;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("Activity Exception:", e);
                return true;
            }
        }
        return false;
    }


    //同步特殊的活動信息,比如商會充值的充值人數,判断红点
    public void syncSpecialActivityData(){
        if(null != baseActivityDataMap) {
            for(BaseActivityData acData : new ArrayList<>(baseActivityDataMap.values())){
                syncUnionRecharge(acData.getActivityId());
            }
        }
    }

    //商会充值活动实时同步
    public void syncUnionRecharge(int activityId){
        BaseActivityData acData = getBaseActivityData(activityId);
        if(acData.getActivityInfo().getType() == eActivityType.UnionActivity.getValue() && acData.getActivityInfo().getChildType() == eUnionActivityType.Recharge.getValue()){
            UnionRechargeUserProto.UnionRechargeUserSyncMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserSyncMsg.newBuilder();
            ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(acData.getActivityInfo().getActivityId());
            if(activityInfo != null){
              /*  UnionActivityConditionData condition = UnionActivityMgr.getUnionActivityConditionData(acData.getActivityInfo().getActivityId(),player.getUserInfo().getUnionUid());
                if(condition != null){
                    respMsg = ActivityPb.parseUnionRechargeUserSyncMsg(acData.getActivityInfo().getActivityId(),condition);
                }
                player.sendChannelPacket(Protocol.U_UNION_ACTIVITY_RECHARGE_USER_SYNC,respMsg);*/
                CrossUnionActivityRankDataProto.CrossUnionRechargeDataReqMsg.Builder reqMsg = CrossUnionActivityRankDataProto.CrossUnionRechargeDataReqMsg.newBuilder();
                reqMsg.setActivityId(activityId);
                reqMsg.setUnionId(player.getUserInfo().getUnionUid());
                reqMsg.setMsgType(1);
                player.sendPacket(CrossProtocol.C_CROSS_UNION_RECHARGE_ACTIVITY_DATA, reqMsg);
            }
        }
    }

    private void syncTurntableLotteryRecord(){
        List<TurntableLotteryRecord> recordList = TurntableMgr.getRecordList();
        player.sendPacket(Protocol.U_TURNTABLE_LOTTERY_RECORD_SYNC, ActivityPb.parseTurntableLotteryRecordListMsg(recordList));
    }

    public void resetOneDay() {
        startChanges();
        Map<Integer, BaseActivityData> copyBaseActivityDataMap = new ConcurrentHashMap<>(baseActivityDataMap);
        for (BaseActivityData baseActivityData : copyBaseActivityDataMap.values()) {
            baseActivityData.resetTask();
            try {
                for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                    if (null != baseActivityCondition) {
                        baseActivityCondition.initPreValue();
                    }
                }
                for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                    if (null != baseActivityCondition) {
                        baseActivityCondition.initValue();
                    }
                }
            }catch (Exception e){
                log.error("resetOneDay init value error:", e);
            }
            try {
                if (baseActivityData.getActivityInfo().getType() == eActivityType.PlatformActivity.getValue() &&
                        baseActivityData.getActivityInfo().getChildType() == 66) {
                    baseActivityData.resetLiveShowTask();
                }
            }catch (Exception e){
                log.error("resetLiveShowTask error:", e);
            }
            try {
                if (baseActivityData.getActivityInfo().getType() == eActivityType.RechargeActivity.getValue() &&
                        baseActivityData.getActivityInfo().getChildType() == 17) {//代金券
                    baseActivityData.resetCouponTask();
                }
            }catch (Exception e){
                log.error("resetCouponTask error:", e);
            }
            try {
                if (baseActivityData.getActivityInfo().getType() == eActivityType.RMBMallActivity.getValue()) {//现金礼包
                    baseActivityData.resetRechargeTriggerTask();
                }
            }catch (Exception e){
                log.error("resetRechargeTriggerTask error:", e);
            }

            try {
                if (baseActivityData.getActivityInfo().getType() == eActivityType.SystemOpenRMBMallActivity.getValue()) {//系统开放现金礼包
                    baseActivityData.resetSystemOpenRechargeTriggerTask();
                }
            }catch (Exception e){
                log.error("resetSystemOpenRechargeTriggerTask error:", e);
            }

        }
        finishChanges();
    }

    public void checkNewActivityData() {
        synchronized (this) {
            startChanges();
            if (NormalActivityMgr.getShowTimeActivityInfoMap() != null) {
                //检查所有活动
                for (ActivityInfo activityInfo : NormalActivityMgr.getShowTimeActivityInfoMap().values()) {
                  /*  if (baseActivityDataMap.containsKey(activityInfo.getActivityId())) {
                        continue;
                    }*/
                    if (ActivityMgr.activityInLoadTime(activityInfo)) {
                        BaseActivityData baseActivityData = baseActivityDataMap.get(activityInfo.getActivityId());
                        if (baseActivityData == null) {
                            addActivityData(activityInfo, new ArrayList<>(), true);
                        }else {
                            addActivityData(activityInfo,baseActivityData.getUserActivityConditionDataList(), false);
                        }
                    }
                }
                if (null != baseActivityDataMap) {
                    for (BaseActivityData baseActivityData : baseActivityDataMap.values()) {
                        if (null == baseActivityData) {
                            continue;
                        }
                        for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                            if (null != baseActivityCondition) {
                                baseActivityCondition.initPreValue();
                            }
                        }
                        for (BaseActivityCondition baseActivityCondition : baseActivityData.getBaseActivityConditionList()) {
                            if (null != baseActivityCondition) {
                                baseActivityCondition.initValue();
                            }
                        }
                    }
                }
            }
            player.notifyListener(eGamePlayerEventType.PaintedEggsEvent.getValue());
            finishChanges();
        }
    }

    private void addActivityData(ActivityInfo activityInfo, List<UserActivityConditionData> userActivityConditionDataList, boolean isNew) {
        synchronized (this) {
            if (null == activityInfo) {
                return;
            }
            if (ActivityMgr.isPatronsAbilityActivityOverTime(player, activityInfo)) {//活动过期
                return;
            }
            List<ActivityConditionInfo> activityConditionInfoList = NormalActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId());
            if (null == activityConditionInfoList) {
                return;
            }
            if (NormalActivityMgr.isMonopolyMarketRankActivity(activityInfo) || activityInfo.getType() == eActivityType.ShuShanActivity.getValue()) {
                if(NormalActivityMgr.isMonopolyMarketRankActivity(activityInfo)){
                    userActivityConditionDataList = MonopolyMarketRankActivityMgr.getUserActivityConditionData(activityInfo.getActivityId(), getUserId());
                }else if(activityInfo.getType() == eActivityType.ShuShanActivity.getValue()){
                    userActivityConditionDataList = ShuShanRankActivityMgr.getUserActivityConditionData(activityInfo, getUserId());
                }
                isNew = false;
            }
            List<BaseActivityCondition> baseActivityConditionList;

            BaseActivityData baseActivityData = baseActivityDataMap.get(activityInfo.getActivityId());
            if (baseActivityData == null) {
                baseActivityConditionList   = new ArrayList<>();
                baseActivityData = new BaseActivityData(player, activityInfo, baseActivityConditionList, userActivityConditionDataList);
                for (UserActivityConditionData data : userActivityConditionDataList) {
                    int conditionId = data.getConditionId();
                    ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfo(activityInfo.getActivityId(), conditionId);
                    if (conditionInfo != null) {
                        BaseActivityCondition baseActivityCondition = BaseActivityCondition.createCondition(activityInfo, conditionInfo, baseActivityData);
                        if(null != baseActivityCondition) {
                            baseActivityConditionList.add(baseActivityCondition);
                        }
                    }
                }
            }else {
                baseActivityConditionList = baseActivityData.getBaseActivityConditionList();
            }
            //获取玩家参与的活动的最高值
            Map<Integer, BigInteger> maxValueMap = baseActivityData.getMaxUserActivityConditionData();
            //遍历该id下所有活动condition
            for (ActivityConditionInfo activityConditionInfo : activityConditionInfoList) {
                if (activityConditionInfo.getType() > 0) {
                    if (baseActivityData.isContinueAddCondition(activityConditionInfo.getType())) {
                        //条件逻辑类
                        BaseActivityCondition baseActivityCondition = BaseActivityCondition.createCondition(activityInfo, activityConditionInfo, baseActivityData);
                        if (null != baseActivityCondition) {
                            //检查玩家的活动条件
                            if (isNew || !baseActivityData.isContainCondition(activityConditionInfo.getConditionId())) {
                                //新的条件
                                baseActivityConditionList.add(baseActivityCondition);
                                UserActivityConditionData userConditionData = NormalActivityMgr.initUserActivityConditionData(player.getUserId(), player.getUserInfo().getNickName(),
                                        BigInteger.ZERO, activityInfo.getActivityId(), activityConditionInfo.getConditionId(), activityConditionInfo.getType());
                                BigInteger maxValue = maxValueMap.get(activityConditionInfo.getType());
                                if (null == maxValue) {
                                    maxValue = BigInteger.ZERO;
                                }
                                userConditionData.setValue(maxValue);
                                baseActivityData.getUserActivityConditionDataList().add(userConditionData);
                            }
                        }
                    }
                }
            }
            baseActivityData.addToPlayer(player);
            baseActivityDataMap.put(activityInfo.getActivityId(), baseActivityData);
        }
    }

    /**
     * 领取冲榜活动奖励
     */
    public ActivityRewardResult getRankActivityReward(int activityId) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        // 判断该任务是否完成
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionData();
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        int myRank = -1;
        //冲榜活动，获取冲榜排名
        boolean isMaster = false;
        UnionActivityConditionData unionActivityConditionData = null;
        if (activityInfo.getType() == eActivityType.RankActivity.getValue()) {
            myRank = RankMgr.getRankListModelByType(eBigRankType.Normal.getValue(), activityId).getMyRank(player.getUserId());
        }else if(activityInfo.getType() == eActivityType.UnionRankActivity.getValue()){
            unionActivityConditionData = UnionActivityMgr.getUnionActivityConditionData(activityId, player.getUserId());
            if(null == unionActivityConditionData){
                result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
                return result;
            }
            myRank = UnionActivityMgr.getMyRank(activityId, unionActivityConditionData.getUnionUid());
            isMaster = (unionActivityConditionData.getUnionBaseInfo().getMasterUserId() == player.getUserId());
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfoByRank(activityId, myRank);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        startChanges();
        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        //拿奖励了
        if(activityInfo.getType() == eActivityType.UnionRankActivity.getValue() && isMaster){
            result.setReward(activityConditionInfo.getMasterRewardStr());
        } else if (activityInfo.getType() == eActivityType.UnionRankActivity.getValue() && unionActivityConditionData != null){
            //职位奖励
            result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(unionActivityConditionData, activityConditionInfo, player.getUserId()));
        } else {
            result.setReward(activityConditionInfo.getRewardStr());
        }
        player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.ActivityReward);
        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), myRank, false, result.getReward());
        baseActivityData.finish(userActivityConditionData.getConditionId());
        onActivityDataChanged(baseActivityData);
        finishChanges();
        if (myRank > 0 && myRank < 4) {//前三
            if (activityInfo.getType() == eActivityType.RankActivity.getValue()) {
                UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
                userHistoryAttribute.addRankActivityTopThreeTotalTimes(activityInfo.getChildType());
                player.notifyListener(eGamePlayerEventType.RankActivityTopThreeTotalTimes.getValue(), userHistoryAttribute);
            } else if (activityInfo.getType() == eActivityType.CrossRankActivity.getValue() &&
                    (activityConditionInfo.getType() == eGamePlayerEventType.AbilityRiseRank.getValue() || activityConditionInfo.getType() == eGamePlayerEventType.IntimacyRiseRank.getValue())) {
                UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
                userHistoryAttribute.addCrossRankActivityTopThreeTotalTimes(activityInfo.getChildType());
                player.notifyListener(eGamePlayerEventType.RankActivityTopThreeTotalTimes.getValue(), userHistoryAttribute);
            }
        }

        return result;
    }

    /**
     * 领取跨服冲榜活动奖励
     */
    public ActivityRewardResult getCrossRankActivityReward(int activityId, int conditionId, boolean isCrossNotify, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if(!isCrossNotify) {
            //发送请求到跨服获取排名
            CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionId(userActivityConditionData.getConditionId());
            player.sendUnionActivityGroupPacket(Protocol.C_CROSS_ACTIVITY_GET_REWARD, reqMsg, reqMsg.getActivityId());
            return result;
        }else {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
            if(null == conditionInfo) {
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            //添加奖励
            startChanges();
            userActivityConditionData.setGetReward(true);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            baseActivityData.finish(userActivityConditionData.getConditionId());
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityReward);
            result.setReward(conditionInfo.getRewardStr());
            //记录跨服称号包含的区服id
            ActivityInfo finalActivityInfo = activityInfo;
            conditionInfo.getReward().getGoods().forEach((k, v)->{
                if (GoodsMgr.isDecorationGoods(k)) {
                    player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                }
            });
            player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(conditionInfo.getRewardStr(), activityInfo.getServerListStr());

            LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();

            if (activityInfo.getType() == eActivityType.CrossRankActivity.getValue()
                    && myRank > 0 && myRank < 4 &&
                    (activityConditionInfo.getType() == eGamePlayerEventType.AbilityRiseRank.getValue() || activityConditionInfo.getType() == eGamePlayerEventType.IntimacyRiseRank.getValue())) {//前三
                UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
                userHistoryAttribute.addCrossRankActivityTopThreeTotalTimes(activityInfo.getChildType());
                player.notifyListener(eGamePlayerEventType.RankActivityTopThreeTotalTimes.getValue(), userHistoryAttribute);
            }
            return result;
        }
    }

    /**
     * 领取新冲榜活动奖励
     */
    public ActivityRewardResult getNewRankActivityReward(int activityId, int conditionId) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(conditionId);
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        int myRank = ActivityRankMgr.getActivityRankListModel(activityId, activityConditionInfo.getType()).getMyRank(player.getUserId());
        activityConditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        startChanges();
        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        //拿奖励了
        result.setReward(activityConditionInfo.getRewardStr());
        player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.ActivityReward);
        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), myRank, false, result.getReward());
        baseActivityData.finish(userActivityConditionData.getConditionId());
        onActivityDataChanged(baseActivityData);
        finishChanges();
        return result;
    }

    /**
     * 领取跨服冲榜活动奖励
     */
    public ActivityRewardResult getNewCrossRankActivityReward(int activityId, int conditionId, boolean isCrossNotify, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(conditionId);
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if(!isCrossNotify) {
            //发送请求到跨服获取排名

            // 商会分组活动走这边
            if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
                builder.setIdKey(String.valueOf(activityConditionInfo.getType()));
                builder.setRewardConditionId(conditionId);
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_TO_REWARD, builder, activityId);
                return result;
            }

            //跨域报名活动
            if(EnterNameActivityMgr.getActivityConfig(activityId) != null){
                player.sendChannelPacket(Protocol.N_ENTER_NAME_GET_RANK_FOR_REWARD,
                        EnterNameActivityMgr.builderRewardRankGet(player.getUserId(), activityId, conditionId, activityConditionInfo.getType()));
                return result;
            }

            CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionId(userActivityConditionData.getConditionId());
            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_GET_RANK_REWARD, reqMsg);
            GamePlayerMgr.sendPacket(player.getUserId(), pkg);
            return result;
        }else {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
            if(null == conditionInfo) {
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            //添加奖励
            startChanges();
            userActivityConditionData.setGetReward(true);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            baseActivityData.finish(userActivityConditionData.getConditionId());
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityReward);
            result.setReward(conditionInfo.getRewardStr());
            //记录跨服称号包含的区服id
            conditionInfo.getReward().getGoods().forEach((k,v)->{
                if (GoodsMgr.isDecorationGoods(k)) {
                    player.getModule(DecorationModule.class).setServerIds(k, activityInfo.getActivityId(), activityInfo.getServerListStr());
                }
            });
            player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(conditionInfo.getRewardStr(), activityInfo.getServerListStr());

            LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();
            return result;
        }
    }

    /**
     * 领取跨服商会争霸活动商会奖励(只有商会奖励)
     */
    public ActivityRewardResult getCrossUnionRankActivityReward(int activityId, int conditionId, boolean isCrossNotify, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);//
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);//结算期
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
      /*  UnionActivityConditionData unionActivityConditionData = UnionActivityMgr.getUnionActivityConditionData(activityId, player.getUserId());
        if (null == unionActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }*/
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
      //  String unionUid = unionActivityConditionData.getUnionUid();
        if(!isCrossNotify) {
            //发送请求到跨服获取排名
           /* CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionId(conditionId);
            reqMsg.setUnionUid(unionUid);
            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_GET_REWARD, reqMsg);
            GamePlayerMgr.sendChannelPacket(player.getUserId(), pkg);*/

            // 商会分组活动走这边
            if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
                builder.setIdKey(String.valueOf(activityConditionInfo.getType()));
                builder.setRewardConditionId(conditionId);
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_TO_REWARD, builder, activityId);
                return result;
            }

            CrossUnionActivityRankDataProto.CrossUnionActivityGetRewardReqMsg.Builder reqMsg = CrossUnionActivityRankDataProto.CrossUnionActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionType(0);
            reqMsg.setConditionId(conditionId);
            reqMsg.setRankType(1);
            player.sendPacket(CrossProtocol.C_CROSS_UNION_ACTIVITY_RANK_REWARD, reqMsg);
            return result;
        }else {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
            if (null == conditionInfo) {
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            //添加奖励
            startChanges();

            userActivityConditionData.setGetReward(true);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            baseActivityData.finish(userActivityConditionData.getConditionId());
            //根据职位获取奖励
            result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(1, conditionInfo, player.getUserId()));
            player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);

            String rewardStr = result.getReward();
            if (rewardStr != null) {
                Property property = PropertyHelper.parseStringToProperty(rewardStr);
                if (property != null) {
                    //记录跨服称号包含的区服id
                    ActivityInfo finalActivityInfo = activityInfo;
                    property.getGoods().forEach((k, v)->{
                        if (GoodsMgr.isDecorationGoods(k)) {
                            player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                        }
                    });
                    player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
                }
            }

            LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();
            return result;
        }
    }


    public ActivityRewardResult getCrossUnionRankActivityReward(int activityId, int conditionId,int myRank,int position,String unionId) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);//
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);//结算期
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
      /*  UnionActivityConditionData unionActivityConditionData = UnionActivityMgr.getUnionActivityConditionData(activityId, player.getUserId());
        if (null == unionActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }*/
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        //  String unionUid = unionActivityConditionData.getUnionUid();

        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
        if (null == conditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        //添加奖励
        startChanges();
        //根据职位获取奖励
        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(userActivityConditionData.getConditionId());

        result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId()));
        player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);

        String rewardStr = result.getReward();
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                ActivityInfo finalActivityInfo = activityInfo;
                property.getGoods().forEach((k, v) -> {
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
                CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo, myRank, unionId, getUserId());
            }
        }
        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();
            return result;
    }

    /**
     * 领取跨服活动队伍榜单奖励
     */
    public ActivityRewardResult getCrossGeneralTeamRankActivityReward(int activityId, int conditionId, boolean isCrossNotify, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);//
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);//结算期
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null == activityConditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if (!isCrossNotify) {
            //发送请求到跨服获取排名
            CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionType(activityConditionInfo.getType());
            reqMsg.setConditionId(conditionId);
            player.sendUnionActivityGroupPacket(Protocol.C2_CROSS_GET_ACTIVITY_TEAM_RANK_REWARD, reqMsg, reqMsg.getActivityId());
            return result;
        } else {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
            if (null == conditionInfo) {
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            //添加奖励
            startChanges();
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityTeamRankReward);
            userActivityConditionData.setGetReward(true);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            baseActivityData.finish(userActivityConditionData.getConditionId());
            result.setReward(conditionInfo.getRewardStr());
            result.setActivityId(activityId);
            result.setConditionId(conditionInfo.getConditionId());

            String rewardStr = result.getReward();
            if (rewardStr != null) {
                Property property = PropertyHelper.parseStringToProperty(rewardStr);
                if (property != null) {
                    //记录跨服称号包含的区服id
                    ActivityInfo finalActivityInfo = activityInfo;
                    property.getGoods().forEach((k, v)->{
                        if (GoodsMgr.isDecorationGoods(k)) {
                            player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                        }
                    });
                    player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr,activityInfo.getServerListStr());
                }
            }
            LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();
            return result;
        }
    }

    /**
     * 通用组队-领取跨服活动队伍榜单奖励
     * @param activityId
     * @param conditionId
     * @param isCrossNotify
     * @param myRank
     * @return
     */
    public ActivityRewardResult geCommonTeamRankActivityReward(int activityId, int conditionId, boolean isCrossNotify, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);//
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);//结算期
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null == activityConditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if (!isCrossNotify) {
            //发送请求到跨服获取排名
            CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionType(activityConditionInfo.getType());
            reqMsg.setConditionId(conditionId);
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_COMMON_TEAM_RECEIVE_TEAM_REWARD, reqMsg, reqMsg.getActivityId());
            return result;
        } else {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
            if (null == conditionInfo) {
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            //添加奖励
            startChanges();
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityTeamRankReward);
            userActivityConditionData.setGetReward(true);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            baseActivityData.finish(userActivityConditionData.getConditionId());
            result.setReward(conditionInfo.getRewardStr());
            result.setActivityId(activityId);
            result.setConditionId(conditionInfo.getConditionId());

            String rewardStr = result.getReward();
            if (rewardStr != null) {
                Property property = PropertyHelper.parseStringToProperty(rewardStr);
                if (property != null) {
                    //记录跨服称号包含的区服id
                    ActivityInfo finalActivityInfo = activityInfo;
                    property.getGoods().forEach((k, v)->{
                        if (GoodsMgr.isDecorationGoods(k)) {
                            player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                        }
                    });
                    player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr,activityInfo.getServerListStr());
                }
            }
            LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();
            return result;
        }
    }


    /**
     * 领取跨服商会榜单奖励
     */
    public ActivityRewardResult getCrossGeneralUnionRankActivityReward(int activityId, int conditionId, boolean isCrossNotify, int myRank) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);//
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);//结算期
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null == activityConditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
       // UnionActivityConditionData unionActivityConditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, activityConditionInfo.getType(), player.getUserId());
        /*if (null == unionActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }*/
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
      //  String unionUid = unionActivityConditionData.getUnionUid();
        if (!isCrossNotify) {
            //发送请求到跨服获取排名

            // 商会分组活动走这边
            if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
                builder.setIdKey(String.valueOf(activityConditionInfo.getType()));
                builder.setRewardConditionId(conditionId);
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_TO_REWARD, builder, activityId);
                return result;
            }

           /* CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionType(activityConditionInfo.getType());
            reqMsg.setConditionId(conditionId);
            reqMsg.setUnionUid(unionUid);
            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_GENERAL_ACTIVITY_GET_RANK_REWARD, reqMsg);
            GamePlayerMgr.sendChannelPacket(player.getUserId(), pkg);*/
            CrossUnionActivityRankDataProto.CrossUnionActivityGetRewardReqMsg.Builder reqMsg = CrossUnionActivityRankDataProto.CrossUnionActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionType(userActivityConditionData.getType());
            reqMsg.setConditionId(conditionId);
            reqMsg.setRankType(9);
            player.sendPacket(CrossProtocol.C_CROSS_UNION_ACTIVITY_RANK_REWARD, reqMsg);
            return result;
        } else {
            ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
            if (null == conditionInfo) {
                result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
                return result;
            }
            //添加奖励
           /* startChanges();
            if (player.getUserId() == unionActivityConditionData.getUnionBaseInfo().getMasterUserId()) {
                player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getMasterReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);
                result.setReward(conditionInfo.getMasterRewardStr());
            } else {
                //根据职位获取奖励
                result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(unionActivityConditionData, conditionInfo, player.getUserId()));
                player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);
            }
            userActivityConditionData.setGetReward(true);
            userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
            baseActivityData.finish(userActivityConditionData.getConditionId());

            String rewardStr = result.getReward();
            if (rewardStr != null) {
                Property property = PropertyHelper.parseStringToProperty(rewardStr);
                if (property != null) {
                    //记录跨服称号包含的区服id
                    ActivityInfo finalActivityInfo = activityInfo;
                    property.getGoods().forEach((k, v)->{
                        if (GoodsMgr.isDecorationGoods(k)) {
                            player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getServerListStr());
                        }
                    });
                    player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
                }
            }
*/

            LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                    activityConditionInfo.getConditionId(), myRank, false, result.getReward());
            onActivityDataChanged(baseActivityData);
            finishChanges();
            return result;
        }
    }


    public ActivityRewardResult getCrossGeneralUnionRankActivityReward(int activityId, int conditionId,  int myRank,int position,String unionId) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);//
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);//结算期
            return result;
        }
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (null == activityConditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_GET_REWARD_CONDITION_ERROR);
            return result;
        }
       /* UnionActivityConditionData unionActivityConditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, activityConditionInfo.getType(), player.getUserId());
        if (null == unionActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }*/
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(activityConditionInfo.getType());
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }

        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, activityConditionInfo.getType(), myRank);
        if (null == conditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        //添加奖励
        startChanges();
        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        //根据职位获取奖励
        result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId()));
        player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);

        baseActivityData.finish(userActivityConditionData.getConditionId());

        String rewardStr = result.getReward();
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                ActivityInfo finalActivityInfo = activityInfo;
                property.getGoods().forEach((k, v) -> {
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
                CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo, myRank, unionId, getUserId());
            }
        }


        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), myRank, false, result.getReward());
        onActivityDataChanged(baseActivityData);
        finishChanges();
        return result;
    }


    /**
     * 领取聯盟奖励
     */
    public ActivityRewardResult getUnionActivityReward(int activityId,int conditionId) {
        long begin = System.currentTimeMillis();
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            result.setRet(GameErrorCode.E_UNION_NO_JOIN_UNION);
            return result;
        }

      /*  UnionActivityConditionData data = UnionActivityMgr.getUnionActivityConditionDataByUnionUid(activityId,unionUid);
        // 判断该任务是否完成
        if (null == baseActivityData || data == null) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }*/
        BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(conditionId);
        if (baseActivityCondition.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if(baseActivityData.canCompleted(conditionId)){
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        //canCompleted没有重写，在这里再判断下
        if(baseActivityCondition.getValue().compareTo(baseActivityCondition.getActivityConditionInfo().getParamList().get(3)) < 0){
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        CrossUnionActivityRankDataProto.CrossUnionRechargeGetRewardMsg.Builder reqMsg = CrossUnionActivityRankDataProto.CrossUnionRechargeGetRewardMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setUnionId(unionUid);
        reqMsg.setConditionId(conditionId);
        reqMsg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        player.sendPacket(CrossProtocol.C_CROSS_UNION_RECHARGE_REWARD, reqMsg);
        //對應得到獎勵領取次數不能超過聯盟的最大人數
        return result;
    }


    public ActivityRewardResult getUnionRechargeActivityReward(int activityId,int conditionId,int getRewardCount) {
        long begin = System.currentTimeMillis();
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            result.setRet(GameErrorCode.E_UNION_NO_JOIN_UNION);
            return result;
        }

        BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(conditionId);
        if (baseActivityCondition.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if(baseActivityData.canCompleted(conditionId)){
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }

        //對應得到獎勵領取次數不能超過聯盟的最大人數

        if(getRewardCount >= CrossUnionMgr.getUnionMaxMemberSize(unionUid)){
            result.setRet(GameErrorCode.E_UNION_ACTIVITY_RECHARGE_USER_MAX);
            return result;
        }
        startChanges();
        //奖励到手
        UnionRechargeUserArgs args = new UnionRechargeUserArgs(UnionRechargeUserArgs.VALUE_IN,0,unionUid,getUserId());
        args.setConditionId(conditionId);
        args.setGetReward(true);
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(conditionId);
        UnionActivityMgr.saveUnionActivityValue(activityId,userActivityConditionData,args);
        UnionActivityMgr.notifyAllMemberActivity(unionUid,getUserId(),eGamePlayerEventType.UnionRechargeUser.getValue());
        //领取奖励红点
        UnionActivityMgr.notifyAllMemberSpecial(unionUid,userActivityConditionData,args);

        String reward = baseActivityCondition.getReward();
        baseActivityData.finish(conditionId);
        onActivityDataChanged(baseActivityData);
        finishChanges();
        result.setReward(reward);
        //商会充值领奖很慢，加个日志
        log.warn("save too long time?, userId {}, module: getUnionActivityReward , cost : {}", getUserId(), System.currentTimeMillis() - begin);
        return result;
    }

    /**
     * 领取活动任务奖励（一键）
     */
    public ActivityRewardResult getActivityRewardOneKey(int activityId, List<Integer> conditionIdList) {
        ActivityRewardResult result = new ActivityRewardResult();

        Property reward = new Property();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        for (UserActivityConditionData conditionData : baseActivityData.getUserActivityConditionDataList()) {
            if(conditionData.isGetReward()){
                continue;
            }
            if(!conditionIdList.contains(conditionData.getConditionId())){
                continue;
            }
            ActivityRewardResult tempResult = getActivityReward(activityId, conditionData.getConditionId());
            if(tempResult.getRet() != 0){
                continue;
            }
            reward.addProperty(PropertyHelper.parseStringToProperty(tempResult.getReward()));
        }

        result.setReward(PropertyHelper.parsePropertyToString(reward));
        return result;
    }

    /**
     * 领取活动奖励
     */
    public ActivityRewardResult getActivityReward(int activityId, int conditionId) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        // 判断该任务是否完成
        if (null == baseActivityData || !baseActivityData.canCompleted(conditionId)) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(conditionId);
        if (null == baseActivityCondition) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        if (baseActivityCondition.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        if(baseActivityData.getActivityInfo().getType() == eActivityType.RechargeActivity.getValue() &&
                baseActivityData.getActivityInfo().getChildType() == 4){//主要限制0元礼包那个白嫖的东西
            if(!ActivityMgr.activityInTime(baseActivityData.getActivityInfo())){
                result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY);
                return result;
            }
        }else if(ActivityMgr.isPatronsAbilityActivityOverTime(player, baseActivityData.getActivityInfo())){//门客赚速（一亿目标）
            result.setRet(GameErrorCode.E_ACTIVITY_OVER_TIME);
            return result;
        } else if (ActivityMgr.isZhanLingActivity(baseActivityData.getActivityInfo())) {
            if (!player.getModule(ZhanLingActivityModule.class).isBuyGift(baseActivityData.getActivityInfo().getActivityId())) {
                result.setRet(GameErrorCode.E_ZHANLING_GIFT_NO_BUY);
            }
        } else if (baseActivityData.getActivityInfo().getType() == eActivityType.NewServerWelfareActivity.getValue()) {
            if (!player.getModule(NewServerWelfareModule.class).checkAndGetReward(baseActivityData.getActivityInfo().getChildType(), baseActivityCondition)) {
                result.setRet(GameErrorCode.E_NEW_SERVER_WELFARE_REWARD_EMPTY);
                return result;
            }
        } else if (baseActivityData.getActivityInfo().getType() == eActivityType.OldPlayerReturn.getValue()) {       //如果是老玩家回归的奖励
            if (baseActivityCondition.getConditionType() != eGamePlayerEventType.LoginDays.getValue()) {
                if (baseActivityCondition.getActivityConditionInfo().getParamList().get(2).intValue() != 0) {
                    UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(eGamePlayerEventType.OldPlayerReturnOfflineDays.getValue());
                    if (conditionData.getParam().isEmpty()) {
                        result.setRet(GameErrorCode.E_PLAYER_RETURN_NOT_RETURN_ROLE);
                        return result;
                    }
                    Long returnTime = Long.valueOf(conditionData.getParam());
                    int nDay = DateHelper.calc2DateTDOADays(new Date(returnTime), new Date());              //计算一下今天是回归第几天
                    if (baseActivityCondition.getActivityConditionInfo().getParamList().get(2).intValue() != nDay) {    //如果回归天数和配置不一样
                        result.setRet(GameErrorCode.E_PLAYER_RETURN_CONDITION_NO_TODAY);
                        return result;
                    }
                }
            }

        }


        startChanges();
        //奖励到手
        String reward = baseActivityCondition.getReward();
        baseActivityData.finish(conditionId);
        onActivityDataChanged(baseActivityData);
        finishChanges();
        result.setReward(reward);

        // 商会七日任务需要记录领奖时的商会
        if (UnionActivityTaskConditionHelper.isUnionActivityConditionType(baseActivityCondition.getConditionType())) {
            String activityUnionUid = GameUnionActivityGroupMgr.getActivityUnionUid(baseActivityData.getActivityInfo(), player);
            baseActivityCondition.setParam(activityUnionUid);
        }
        syncGetOldPlayerBindReward(baseActivityCondition, baseActivityData);
        //限时奖励活动-埋点
        DataAnalyticsMgr.trackTaskActivity(player, conditionId, baseActivityCondition.getConditionType());
        return result;
    }


    /**
     * 领取 海上争霸 之南海丝路 联盟排行榜奖励
     * @param activityId
     * @param conditionType
     * @param myRank
     * @param masterId
     * @return
     */
    public ActivityRewardResult receiveSouthSeaUnionRankReward(int activityId, int conditionType, int myRank, long masterId,String unionId){
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId,conditionType, myRank);
        if (null == conditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }

        //添加奖励

        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(userActivityConditionData.getConditionId());

        startChanges();
        if (player.getUserId() == masterId) {
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getMasterReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);
            result.setReward(conditionInfo.getMasterRewardStr());
        } else {
            //根据职位获取奖励
            result.setReward(conditionInfo.getRewardStr());
            player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);
        }

        String rewardStr = result.getReward();
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                ActivityInfo finalActivityInfo = activityInfo;
                property.getGoods().forEach((k, v)->{
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
                CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo,myRank,unionId,getUserId());
            }
        }


        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                conditionInfo.getConditionId(), myRank, false, result.getReward());
        onActivityDataChanged(baseActivityData);
        finishChanges();
        return result;
    }

    /**
     * 领取 巅峰海上争霸 联盟排行榜奖励
     * @param activityId
     * @param conditionType
     * @param myRank
     * @param position
     * @return
     */
    public ActivityRewardResult receivePeakSeacraftUnionRankReward(int activityId, int conditionType, int myRank, int position,String unionId){
        ActivityRewardResult result = new ActivityRewardResult();
        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId,conditionType, myRank);
        if (null == conditionInfo) {
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (null == userActivityConditionData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }

        String reward = UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId());
        //添加奖励
        startChanges();

        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(userActivityConditionData.getConditionId());

        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.CrossActivityUnionRankReward);
        result.setReward(reward);

        String rewardStr = result.getReward();
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                ActivityInfo finalActivityInfo = activityInfo;
                property.getGoods().forEach((k, v)->{
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, finalActivityInfo.getActivityId(), finalActivityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
                CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo,myRank,unionId,getUserId());
            }
        }


        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                conditionInfo.getConditionId(), myRank, false, result.getReward());
        onActivityDataChanged(baseActivityData);
        finishChanges();
        return result;
    }


    private void syncGetOldPlayerBindReward(BaseActivityCondition baseActivityCondition, BaseActivityData baseActivityData) {
        if (baseActivityCondition.getConditionType() == eGamePlayerEventType.OldPlayerReturnBindRole.getValue()) {
            String language = player.getLanguage();
            player.sendPacket(Protocol.U_ACTIVITY_SYNC, ActivityPb.parseActivityDataListMsg(Collections.singletonList(baseActivityData),
                    null, null, null, null, language));
        }
    }

    /**
     * 一键领取活动奖励
     */
    public ActivityRewardResult getAllActivityReward(int activityId) {
        ActivityRewardResult result = new ActivityRewardResult();
        result.setConditionIdList(new ArrayList<>());
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if(baseActivityData != null){
            Property reward = new Property();
            List<UserActivityConditionData> activityConditionData = baseActivityData.getUserActivityConditionDataList();
            for(UserActivityConditionData tempData : activityConditionData){
                if(baseActivityData.canCompleted(tempData.getConditionId())){
                    BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(tempData.getConditionId());
                    if (!baseActivityCondition.isGetReward()) {
                        //奖励到手
                        reward.addProperty(PropertyHelper.parseStringToProperty(baseActivityCondition.getReward()));
                        baseActivityData.finish(tempData.getConditionId());
                        tempData.setGetReward(true);
                        result.getConditionIdList().add(tempData.getConditionId());
                    }
                }
            }
            result.setReward(PropertyHelper.parsePropertyToString(reward));
        }
        if(result.getConditionIdList().size() > 0){
            List<BaseActivityData> abd = new ArrayList<>();
            abd.add(baseActivityData);
            syncActivity(abd);
        }
        return result;
    }

    public ActivityRewardResult getMultipleActivityReward(int activityId,List<Integer> conditionIdList) {
        ActivityRewardResult result = new ActivityRewardResult();
        result.setConditionIdList(new ArrayList<>());
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if(baseActivityData != null){
            Property reward = new Property();
            for(int conditionId : conditionIdList){
                if(!baseActivityData.canCompleted(conditionId)){
                    result.setRet(GameErrorCode.E_ACTIVITY_MULTIPLE_GET_REWARD_SOME_ISGET);
                    return result;
                }
            }
            for(int conditionId : conditionIdList){
                if(baseActivityData.canCompleted(conditionId)){
                    UserActivityConditionData activityConditionData = baseActivityData.getUserActivityConditionData(conditionId);
                    BaseActivityCondition baseActivityCondition = baseActivityData.getBaseActivityCondition(conditionId);
                    if (!baseActivityCondition.isGetReward()) {
                        //奖励到手
                        reward.addProperty(PropertyHelper.parseStringToProperty(baseActivityCondition.getReward()));
                        baseActivityData.finish(conditionId);
                        activityConditionData.setGetReward(true);
                        result.getConditionIdList().add(conditionId);
                    }
                }
            }
            result.setReward(PropertyHelper.parsePropertyToString(reward));
        }
        if(result.getConditionIdList().size() > 0){
            List<BaseActivityData> abd = new ArrayList<>();
            abd.add(baseActivityData);
            syncActivity(abd);
        }
        return result;
    }

    public void nickNameChange(){
        for(BaseActivityData activityData : baseActivityDataMap.values()){
            if(ActivityMgr.activityInShowTime(activityData.getActivityInfo())) {
                if (activityData.getActivityInfo().getType() == eActivityType.UnionRankActivity.getValue()) {
                    List<UserActivityConditionData> dataList = activityData.getUserActivityConditionDataList();
                }
            }
        }
    }

    public int queryActivityMallCanBuy(int activityId, int conditionId) {
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        // 判断该任务是否完成
        if (null == baseActivityData) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if(!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if(activityInfo.getType() != eActivityType.RechargeActivity.getValue() &&
                activityInfo.getType() != eActivityType.RMBMallActivity.getValue() &&
                activityInfo.getType() != eActivityType.SystemOpenRMBMallActivity.getValue() &&
                !ActivityMgr.isNewRechargeActivity(activityId)
        ){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(conditionId);
        if(null == userActivityConditionData){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if(activityInfo.getType() == eActivityType.RMBMallActivity.getValue()){
            if(userActivityConditionData.isGetReward()){
                return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            }
        } else if (ActivityMgr.isNewRechargeActivity(activityId)) {
            if (userActivityConditionData.getValue().compareTo(BigInteger.ONE) >= 0) {
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        } else if (activityInfo.getType() == eActivityType.SystemOpenRMBMallActivity.getValue()) {
            if(userActivityConditionData.isGetReward()){
                return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            }
        } else {
            ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityId, conditionId);
            if (null == activityConditionInfo) {
                return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
            }
            if (userActivityConditionData.getValue().compareTo(activityConditionInfo.getParamList().get(1)) >= 0) {//限购
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        }
        return 0;
    }

    public BaseActivityData getBaseActivityData(int activityId) {
        return baseActivityDataMap.get(activityId);
    }

    /**
     * 单条执行：添加到临时列表；并通知更新客户端数据
     */
    public final void onActivityDataChanged(BaseActivityData baseActivityData) {
        if (null == baseActivityData) {
            return;
        }
        if (!changeBaseActivityDataList.contains(baseActivityData)) {
            synchronized (changeBaseActivityDataList) {
                changeBaseActivityDataList.add(baseActivityData);
            }
        }

        if (changeBaseActivityCount.intValue() <= 0 && changeBaseActivityDataList.size() > 0 || changeBaseActivityDataList.size() > 20) {
            updateChangedActivityData();
        }
    }

    //后面再一次同步
    public final void onActivityDataChangedNoSync(BaseActivityData baseActivityData) {
        if (null == baseActivityData) {
            return;
        }
        if (!changeBaseActivityDataList.contains(baseActivityData)) {
            synchronized (changeBaseActivityDataList) {
                changeBaseActivityDataList.add(baseActivityData);
            }
        }
    }

    /**
     * 开始变更，改变标识
     */
    public void startChanges() {
        changeBaseActivityCount.getAndIncrement();
    }

    /**
     * 完成变更
     */
    public void finishChanges() {
        synchronized (changeBaseActivityCount) {
            int changes = changeBaseActivityCount.decrementAndGet();
            if (changes < 0) {
                log.error("not exce startChanges" + new Error());
                changeBaseActivityCount.set(0);
            }
            if (changes <= 0 && changeBaseActivityDataList.size() > 0) {
                updateChangedActivityData();
            }
//            if (changeBaseActivityDataList.size() > 0) {
//                updateChangedActivityData();
//            }
        }
    }

    /**
     * 变更下发给客户端
     */
    private void updateChangedActivityData() {
        if (changeBaseActivityDataList.size() > 0) {
            List<BaseActivityData> changedList;
            synchronized (changeBaseActivityDataList) {
                changedList = new ArrayList<>(changeBaseActivityDataList);
                changeBaseActivityDataList.clear();
            }
            syncActivity(changedList);
        }
    }
    public void syncChangedActivityData() {
        if (changeBaseActivityDataList.size() > 0) {
            List<BaseActivityData> changedList;
            synchronized (changeBaseActivityDataList) {
                changedList = new ArrayList<>(changeBaseActivityDataList);
                changeBaseActivityDataList.clear();
            }
            syncActivity(changedList);
        }
    }

    private void syncActivity(List<BaseActivityData> baseActivityDataList) {
        if (null == baseActivityDataList) {
            return;
        }
        String language = player.getLanguage();
        player.sendPacket(Protocol.U_ACTIVITY_SYNC, ActivityPb.parseActivityDataListMsg(baseActivityDataList,
                null, null, null, null, language));
    }

    public synchronized void initRechargeConditionData() {
        for (BaseActivityData baseActivityData : baseActivityDataMap.values()) {
            baseActivityData.initRechargeConditionData();
        }
    }

    public boolean isUnlockEarnSpeedPlusFund(ActivityConditionInfo activityConditionInfo) {
        BaseActivityData activityData = player.getModule(ActivityModule.class).getBaseActivityData(activityConditionInfo.getActivityId());
        if (activityData == null) {
            return player.getUserInfo().getEarnSpeed().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
        }
        UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(activityConditionInfo.getConditionId());
        return userActivityConditionData.getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    public boolean isUnlockBeautyHigerFund(ActivityConditionInfo activityConditionInfo) {
        BaseActivityData activityData = player.getModule(ActivityModule.class).getBaseActivityData(activityConditionInfo.getActivityId());
        if (activityData == null) {
            return player.getModule(BeautyModule.class).getBeautyCount() >= activityConditionInfo.getParamList().get(3).intValue();
        }
        UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(activityConditionInfo.getConditionId());
        return userActivityConditionData.getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    public List<UserActivityConditionData> getRechargeConditionTypeList(int type, List<UserActivityConditionData> dataList) {
        Map<Integer, UserActivityConditionData> typeMap = new HashMap<>();
        for (UserActivityConditionData data : dataList) {
            if (type == data.getType()) {
                continue;
            }
            if (!typeMap.containsKey(data.getType())) {
                typeMap.put(data.getType(), data);
                continue;
            }
            UserActivityConditionData conditionData = typeMap.get(data.getType());
            if (!conditionData.isGetReward() && data.getValue().intValue() > 0) {
                typeMap.put(data.getType(), data);
                continue;
            }
            if (data.isGetReward()) {
                int thisGrade = NormalActivityMgr.getGradeByMallId(data.getActivityId(), data.getValue().intValue());
                int lastGrade = NormalActivityMgr.getGradeByMallId(conditionData.getActivityId(), conditionData.getValue().intValue());
                if (thisGrade > lastGrade) {
                    typeMap.put(data.getType(), data);
                }
            }
        }
        return new ArrayList<>(typeMap.values());
    }

    public ActivityRewardResult getUnionRankActivityReward(int activityId) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        // 判断该任务是否完成
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionData();
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }

        CrossUnionActivityRankDataProto.CrossUnionRankRewardReqMsg.Builder reqMsg = CrossUnionActivityRankDataProto.CrossUnionRankRewardReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        player.sendPacket(CrossProtocol.C_CROSS_UNION_RANK_REWARD, reqMsg);
        return result;
    }

    public ActivityRewardResult getUnionRankActivityRewardFromCross(int activityId,int myRank,int position) {
        ActivityRewardResult result = new ActivityRewardResult();
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        // 判断该任务是否完成
        if (null == baseActivityData) {
            result.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return result;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            result.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            return result;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            result.setRet(GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME);
            return result;
        }
        UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionData();
        if(null == userActivityConditionData){
            result.setRet(GameErrorCode.E_ACTIVITY_NO_JOIN);
            return result;
        }
        if (userActivityConditionData.isGetReward()) {
            result.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return result;
        }
        result.setConditionId(userActivityConditionData.getConditionId());

        //冲榜活动，获取冲榜排名
        ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfoByRank(activityId, myRank);
        if(null == activityConditionInfo){
            result.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return result;
        }
        startChanges();
        userActivityConditionData.setGetReward(true);
        userActivityConditionData.setModifyTime(System.currentTimeMillis() / 1000);
        //拿奖励了
        result.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(position, activityConditionInfo, player.getUserId()));
        player.getModule(CurrencyModule.class).addCurrency(result.getReward(), eLogMoneyType.Activity, eLogMoneyType.ActivityReward);
        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), myRank, false, result.getReward());
        baseActivityData.finish(userActivityConditionData.getConditionId());
        onActivityDataChanged(baseActivityData);
        finishChanges();
        if (myRank > 0 && myRank < 4) {//前三
            if (activityInfo.getType() == eActivityType.RankActivity.getValue()) {
                UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
                userHistoryAttribute.addRankActivityTopThreeTotalTimes(activityInfo.getChildType());
                player.notifyListener(eGamePlayerEventType.RankActivityTopThreeTotalTimes.getValue(), userHistoryAttribute);
            } else if (activityInfo.getType() == eActivityType.CrossRankActivity.getValue() &&
                    (activityConditionInfo.getType() == eGamePlayerEventType.AbilityRiseRank.getValue() || activityConditionInfo.getType() == eGamePlayerEventType.IntimacyRiseRank.getValue())) {
                UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
                userHistoryAttribute.addCrossRankActivityTopThreeTotalTimes(activityInfo.getChildType());
                player.notifyListener(eGamePlayerEventType.RankActivityTopThreeTotalTimes.getValue(), userHistoryAttribute);
            }
        }
        return result;
    }

    class DataSort implements Comparator<UserActivityConditionData> {
        @Override
        public int compare(UserActivityConditionData o1, UserActivityConditionData o2) {
            return getRMBMallBeginTime(o2).compareTo(getRMBMallBeginTime(o1));
        }
    }

    public BigInteger getRMBMallBeginTime(UserActivityConditionData data) {
        BigInteger beginTime;
        if (!data.getParam().isEmpty() && data.getPreValue().intValue() == -2) {
            ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(data.getActivityId());
            int rmbMallOverTime = NormalActivityMgr.getRMBMallOverTime(activityInfo);
            beginTime = new BigInteger(data.getParam()).subtract(BigInteger.valueOf(rmbMallOverTime));
        } else {
            beginTime = data.getPreValue();
        }
        return beginTime;
    }

    /**
     * 获取系统开放现金礼包的商品
     */
    public GetRechargeMallResult getSystemOpenShowRMBMallInfo(ActivityConditionInfo activityConditionInfo) {
        GetRechargeMallResult result = new GetRechargeMallResult();
        int mallParamId = activityConditionInfo.getParamList().get(2).intValue();
        MallInfo mallInfo = NormalActivityMgr.getSystemOpenRMBActivityMallInfoByGrade(activityConditionInfo.getActivityId(), mallParamId);
        result.setMallInfo(mallInfo);
        return result;
    }

    /**
     * 获取现金礼包的商品
     */
    public GetRechargeMallResult getShowRMBMallInfo(ActivityConditionInfo activityConditionInfo) {
        GetRechargeMallResult result = new GetRechargeMallResult();
        List<Integer> mallGradeList = StringUtils.stringToIntegerList(activityConditionInfo.getExtendParam(), "\\|");
        BaseActivityData activityData = player.getModule(ActivityModule.class).getBaseActivityData(activityConditionInfo.getActivityId());
        List<UserActivityConditionData> dataList = getRechargeConditionTypeList(activityConditionInfo.getType(), activityData.getUserActivityConditionDataList());
        dataList.sort(new DataSort());
        boolean isLastBuy = NormalActivityMgr.isShowRMBMall(dataList);
        MallInfo mallInfo = null;
        if (isLastBuy) {//判断升降档
            //升档
            if (dataList.size() > 0) {
                UserActivityConditionData firstData = dataList.get(0);
                if (firstData.getValue().compareTo(BigInteger.ZERO) > 0) {
                    if (firstData.isGetReward()) {//最近的礼包有购买，升档处理
                        mallInfo = NormalActivityMgr.getMallInfoByLastMall(player, firstData, activityConditionInfo, mallGradeList, 1);
                    } else {//没购买需要判断是否过了冷却时间
                        boolean needDown = false;    //需要降档
                        if (firstData.getPreValue().intValue() != -2) {
                            long nowTime = System.currentTimeMillis() / 1000;
                            if (activityConditionInfo.getType() != eGamePlayerEventType.RechargeTitle.getValue() && nowTime - firstData.getPreValue().longValue() < NormalActivityMgr.getRMBMallCDTime(activityData.getActivityInfo())) {
                                result.setDuringCd(true);
                                return result;          //没过冷却时间
                            }
                            if (nowTime > Long.parseLong(firstData.getParam())) {//超过到期时间还没买，降档
                                needDown = true;
                            } else {
                                if (dataList.size() > 1) {
                                    UserActivityConditionData secondData = dataList.get(1);
                                    if (secondData.getValue().compareTo(BigInteger.ZERO) > 0) {
                                        if (!secondData.isGetReward()) {//第二个礼包也没购买
                                            needDown = true;
                                        }
                                    }
                                }
                            }
                        }
                        if (needDown) {//需要降档
                            long baseIndex = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.RechargeBaseIndex);
                            baseIndex = baseIndex <= 1 ? baseIndex : baseIndex - 1;
                            player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.RechargeBaseIndex, baseIndex);
                            setDownStateParam(firstData);
                            mallInfo = NormalActivityMgr.getMallInfoByLastMall(player, firstData, activityConditionInfo, mallGradeList, 2);
                        } else {
                            mallInfo = NormalActivityMgr.getMallInfoByLastMall(player, firstData, activityConditionInfo, mallGradeList, 0);
                        }
                    }
                }
            }
        } else {//找近7日的订单价位
            long lastSevenDayPrice = player.getModule(RechargeModule.class).lastSevenDayOrderPrice();
            mallInfo = RechargeManager.getMallInfoByPrice(player, lastSevenDayPrice, activityConditionInfo, mallGradeList);
        }
        result.setMallInfo(mallInfo);
        return result;
    }

    private void setDownStateParam(UserActivityConditionData firstData) {
        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(firstData.getActivityId());
        for (UserActivityConditionData userActivityConditionData : baseActivityData.getUserActivityConditionDataList()) {
            if (userActivityConditionData.getType() == firstData.getType()) {
                userActivityConditionData.setPreValue(BigInteger.valueOf(-2));
            }
        }
    }

    /**
     * 检查商会condition值是不是商会最新的
     */
    public void checkUnionActivityData() {
        UnionActivityTaskMgr.loginRefreshValue(player.getUserId());
    }

    /**
     * 补签，增加补签天数
     * @param activityId
     * @param mallInfo
     */
    public void doReissueAddDays(int activityId, MallInfo mallInfo){
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(null == activityInfo){
            return;
        }
        if(activityInfo.getType() != eActivityType.RechargeActivity.getValue() ||
                activityInfo.getChildType() != eActivityChargeType.CanReissueDaysCharge.getChildType()){//新累日充值活动
            return;
        }
        if(!ActivityMgr.isInRechargeActivityReissueTime(activityInfo)){//不在补签时间内
            return;
        }
        if(null == mallInfo){
            return;
        }
        if(mallInfo.getCategory() != eMallCategory.ReissueMall.getValue()){//补签商品
            return;
        }
        //增加补签天数
        player.notifyListener(eGamePlayerEventType.RechargeDays.getValue(), new RechargeDaysArgs(1, true));
    }

    /**
     * 能够补签
     * @param activityInfo
     * @return
     */
    public boolean canReissue(ActivityInfo activityInfo){
        if(null == activityInfo){
            return false;
        }
        List<ActivityConditionInfo> conditionInfoList = NormalActivityMgr.getActivityConditionInfoListByType(activityInfo.getActivityId(),
                eGamePlayerEventType.RechargeDays.getValue());
        int maxReissueDays = 0;     //最多可以签到天数
        if(null != conditionInfoList) {
            for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                int param4 = conditionInfo.getParamList().get(3).intValue();
                if (param4 > maxReissueDays) {
                    maxReissueDays = param4;
                }
            }
        }
        int hasSignInDays = 0;      //已经签到天数
        BaseActivityData baseActivityData = getBaseActivityData(activityInfo.getActivityId());
        if(null != baseActivityData){
            for(UserActivityConditionData conditionData : baseActivityData.getUserActivityConditionDataList()){
                if(conditionData.getValue().intValue() > hasSignInDays){
                    hasSignInDays = conditionData.getValue().intValue();
                }
            }
        }
        if(hasSignInDays >= maxReissueDays){
            return false;
        }
        String[] paramArr = activityInfo.getParam6().split("\\|");
        int needDays = StringUtils.string2intSafe(paramArr[1], 0);
        if(hasSignInDays < needDays){
            return false;
        }
        int nowMaxReissueDays = DateHelper.calc2DateTDOADays(new Date(activityInfo.getBeginTime() * 1000), new Date());
        if(hasSignInDays >= nowMaxReissueDays){
            return false;
        }
        if(hasSignInDays + 1 == nowMaxReissueDays){
            long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
            long lastRechargeTime = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.LastRechargeTime);
            if(lastRechargeTime < zeroTime){//今日未充值
                return false;
            }
        }
        return true;
    }

    /**
     * 获取XRank排行榜奖励
     */
    public int getXRankReward(int activityId, int conditionType, int myRank, eRankInfo rankInfo) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);
        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        // 是否masterReward
        boolean isMasterReward = isXRankMasterReward(activityId, rankInfo);
        String rewardStr = isMasterReward ? conditionInfo.getMasterRewardStr() : conditionInfo.getRewardStr();
        Property reward = isMasterReward ? conditionInfo.getMasterReward() : conditionInfo.getReward();

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.XRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        XRankProto.GetXRankRewardRespMsg.Builder builder = XRankProto.GetXRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_XRANK_GET_REWARD, builder);
        return 0;
    }

    /**
     * 五熊争霸领奖
     */
    public int getFiveHeroRankReward(int activityId, int conditionType, int myRank, int position, int campRank,String unionId) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo;
        if (conditionType == eGamePlayerEventType.FiveHeroCampUnionPopularityRank.getValue()) {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoBy2Rank(activityId, conditionType, campRank, myRank);
        } else {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);
        }
        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        Property reward = new Property();
        String rewardStr = "";
        // 是否masterReward
        if(conditionType == eGamePlayerEventType.FiveHeroCampUnionPopularityRank.getValue()){
            rewardStr = UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId());
            reward = PropertyHelper.parseStringToProperty(rewardStr);
        }else{
            boolean isMasterReward = position == 1 && eGamePlayerEventType.FiveHeroCampUnionPopularityRank.getValue() == conditionType;
            rewardStr = isMasterReward ? conditionInfo.getMasterRewardStr() : conditionInfo.getRewardStr();
            reward = isMasterReward ? conditionInfo.getMasterReward() : conditionInfo.getReward();
        }

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.FiveHeroGetRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());
        // 加荣誉点
        CrossUnionMgr.getRewardHonourItem(rewardStr, baseActivityData.getActivityInfo(), myRank, unionId, getUserId());

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        FiveHeroProto.FiveHeroGetRankRewardRespMsg.Builder builder = FiveHeroProto.FiveHeroGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_FIVEHERO_GET_RANK_REWARD, builder);
        return 0;
    }
    /**
     * 统兵演武榜单领奖
     */
    public int getTongBingRankReward(int activityId, int conditionType, int myRank, int position, int campRank, String unionUid) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo;
        if (conditionType == eGamePlayerEventType.TongBingCampUnionZhanGongRank.getValue()) {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoBy2Rank(activityId, conditionType, campRank, myRank);
        } else {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);
        }
        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        // 是否masterReward
        boolean isMasterReward = position == 1 && eGamePlayerEventType.TongBingCampUnionZhanGongRank.getValue() == conditionType;
        String rewardStr = isMasterReward ? conditionInfo.getMasterRewardStr() : conditionInfo.getRewardStr();
        Property reward = isMasterReward ? conditionInfo.getMasterReward() : conditionInfo.getReward();

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.TongBingRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());
        // 加荣誉点
        CrossUnionMgr.getRewardHonourItem(rewardStr, baseActivityData.getActivityInfo(), myRank, unionUid, getUserId());

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        TongBingProto.TongBingGetRankRewardRespMsg.Builder builder = TongBingProto.TongBingGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_TONG_BING_GET_RANK_REWARD, builder);
        return 0;
    }
    /**
     * 巅峰统兵演武榜单领奖
     */
    public int getTonBingRankReward(int activityId, int conditionType, int myRank, int position, int campRank,String unionId) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo;
        if (conditionType == eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue()) {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoBy2Rank(activityId, conditionType, campRank, myRank);
        } else {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);
        }
        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        Property reward = new Property();
        String rewardStr = "";
        // 是否masterReward
        if(conditionType == eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue()){
            rewardStr = UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId());
            reward = PropertyHelper.parseStringToProperty(rewardStr);
        }else{
            boolean isMasterReward = position == 1 && eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue() == conditionType;
            rewardStr = isMasterReward ? conditionInfo.getMasterRewardStr() : conditionInfo.getRewardStr();
            reward = isMasterReward ? conditionInfo.getMasterReward() : conditionInfo.getReward();
        }

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.TonBingRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(activityInfo != null){
            CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo,myRank,unionId,getUserId());
        }else{
            log.error("巅峰统兵出现找不到活动领取商会排行奖励");
        }

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        TongBingProto.TongBingGetRankRewardRespMsg.Builder builder = TongBingProto.TongBingGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_TON_BING_GET_RANK_REWARD, builder);
        return 0;
    }

    /**
     * 巅峰统兵演武榜单领奖
     */
    public int getChuHanRankReward(int activityId, int conditionType, int myRank, int position, int campRank, String unionId) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo;
        if (conditionType == eGamePlayerEventType.ChuHanCampUnionZhanGongRank.getValue()) {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoBy2Rank(activityId, conditionType, campRank, myRank);
        } else {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);
        }
        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        Property reward = new Property();
        String rewardStr = "";
        // 是否masterReward
        if (conditionType == eGamePlayerEventType.ChuHanCampUnionZhanGongRank.getValue()) {
            rewardStr = UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId());
            reward = PropertyHelper.parseStringToProperty(rewardStr);
        } else {
            boolean isMasterReward = position == 1 && eGamePlayerEventType.ChuHanCampUnionZhanGongRank.getValue() == conditionType;
            rewardStr = isMasterReward ? conditionInfo.getMasterRewardStr() : conditionInfo.getRewardStr();
            reward = isMasterReward ? conditionInfo.getMasterReward() : conditionInfo.getReward();
        }

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.ChuHan, eLogMoneyType.ChuHanRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo, myRank, unionId, getUserId());
        } else {
            log.error("楚汉争霸出现找不到活动领取商会排行奖励");
        }

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        ChuHanProto.ChuHanGetRankRewardRespMsg.Builder builder = ChuHanProto.ChuHanGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(Protocol.S_CHUHAN_GET_RANK_REWARD % 20000, builder);
        return 0;
    }

    public int getChannelTonBingRankReward(int activityId, int conditionType, int myRank, int position, int campRank, String unionId) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo;
        if (conditionType == eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue()) {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoBy2Rank(activityId, conditionType, campRank, myRank);
        } else {
            conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);
        }
        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        Property reward = new Property();
        String rewardStr = "";
        // 是否masterReward
        if(conditionType == eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue()){
            rewardStr = UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId());
            reward = PropertyHelper.parseStringToProperty(rewardStr);
        }else{
            boolean isMasterReward = position == 1 && eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue() == conditionType;
            rewardStr = isMasterReward ? conditionInfo.getMasterRewardStr() : conditionInfo.getRewardStr();
            reward = isMasterReward ? conditionInfo.getMasterReward() : conditionInfo.getReward();
        }

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.TonBingRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(activityInfo != null){
            CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo,myRank,unionId,getUserId());
        }else{
            log.error("巅峰统兵出现找不到活动领取商会排行奖励");
        }

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        ChannelTonBingProto.CTonBingGetRankRewardRespMsg.Builder builder = ChannelTonBingProto.CTonBingGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_CTON_BING_GET_RANK_REWARD, builder);
        return 0;
    }


    /**
     * 凡人修仙榜单领奖
     */
    public int getXiuXianRankReward(int activityId, int conditionType, int myRank) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);

        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        String rewardStr = conditionInfo.getRewardStr();
        Property reward = conditionInfo.getReward();

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.XiuXianActivityRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        XiuXianProto.XiuXianGetRankRewardRespMsg.Builder builder = XiuXianProto.XiuXianGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_XIU_XIAN_GET_RANK_REWARD, builder);
        return 0;
    }

    /**
     * 山河乐宴榜单领奖
     */
    public int getFeastRankReward(int activityId, int conditionType, int myRank) {
        // 是否有活动数据
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否有该排行榜condition
        UserActivityConditionData conditionData = baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
        if (conditionData == null) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 排名是否有奖励
        ActivityConditionInfo conditionInfo = NormalActivityMgr.getActivityConditionInfoByRankAndType(activityId, conditionType, myRank);

        if (null == conditionInfo) {
            return GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
        }
        // 是否领过奖
        if (conditionData.isGetReward()) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }

        String rewardStr = conditionInfo.getRewardStr();
        Property reward = conditionInfo.getReward();

        startChanges();
        conditionData.setGetReward(true);
        conditionData.setModifyTime(System.currentTimeMillis() / 1000);
        baseActivityData.finish(conditionData.getConditionId());
        // 奖励加背包
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.FeastActivityRankReward);
        // 记录跨服称号包含的区服id
        reward.getGoods().forEach((k, v) -> {
            if (GoodsMgr.isDecorationGoods(k)) {
                player.getModule(DecorationModule.class).setServerIds(k, baseActivityData.getActivityInfo().getActivityId(), baseActivityData.getActivityInfo().getServerListStr());
            }
        });
        // 记录宴会帖
        player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, baseActivityData.getActivityInfo().getServerListStr());

        LogMgr.addLogActivityReward(player.getUserId(), activityId, baseActivityData.getActivityInfo().getType(), baseActivityData.getActivityInfo().getChildType(),
                conditionInfo.getConditionId(), myRank, false, rewardStr);
        onActivityDataChanged(baseActivityData);
        finishChanges();

        // 返回
        ShanHeFeastProto.FeastGetRankRewardRespMsg.Builder builder = ShanHeFeastProto.FeastGetRankRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(rewardStr);
        player.sendPacket(ClientProtocol.U_SHAN_HE_FEAST_GET_RANK_REWARD, builder);
        return 0;
    }


    private boolean isXRankMasterReward(int activityId, eRankInfo rankInfo) {
        boolean isMasterReward = false;

        if (rankInfo.getActivityType() == eActivityType.ChannelRankActivity && rankInfo.getRangeType() == eRankRangeType.Channel) {
            if (!ActivityAbleMgr.isAble(getUserId(), activityId)) {
                isMasterReward = true; // 没资格的，地区榜拿masterReward
            }
        }

        return isMasterReward;
    }

    /**
     * 获取榜单condition
     */
    public UserActivityConditionData getUserRankCondition(int activityId, int conditionType) {
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return null;
        }
        return baseActivityData.getFirstUserActivityConditionDataByType(conditionType);
    }

    public void changeUnionRechargeData(int activityId, String info,int type) {
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData != null) {
            List<UserActivityConditionData> conditionDataList = baseActivityData.getUserActivityConditionDataList();
            if (conditionDataList != null) {
                for (UserActivityConditionData data : conditionDataList) {
                    UnionRechargeUserArgs args = new UnionRechargeUserArgs(type,0,"",0);
                    args.setConditionId(data.getConditionId());
                    data.setValue(args.getFinalValue(info));
                }
            }
            onActivityDataChanged(baseActivityData);
        }
    }

    public void resetReturnTask(int activityId) {
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return;
        }
        baseActivityData.resetReturnTask();
    }

    /**
    * 描述：同步攻略屏蔽
    * 作者：zrq
    * 时间：2023/4/20
    * 参数：

    * 返回值：
    **/
    public void syncIntroductionShield(){
        int clientType = 0;
        if (null != player.getAppExt() && player.getAppExt().containsKey("isH5")) {
            clientType = player.getAppExt().getIntValue("isH5");
        }
        List<IntroductionShieldConfig> dataList = IntroductionShieldMgr.getShieldList(player.getAppId(), clientType);
        ActivityProto.IntroductionShieldListMsg.Builder listMsg = ActivityPb.parseIntroductionShieldListMsg(dataList);
        player.sendPacket(Protocol.U_INTRODUCTION_SHIELD_SYNC, listMsg);
    }

    public void resetCricketCourtTokenTask(int activityId) {
        BaseActivityData baseActivityData = getBaseActivityData(activityId);
        if (baseActivityData == null) {
            return;
        }
        baseActivityData.resetCricketCourtTokenTask();
    }
}
